#-*- coding: utf-8 -*-
import datetime

from django.db import models

# Create your models here.

#Blog类别
class Category(models.Model):
    #类别
    category = models.CharField(max_length = 30)

    def __unicode__(self):
        """
        
        Arguments:
        - `self`:
        """
        return self.category

#我的链接    
class Link(models.Model):
    #链接所有人的姓名
    user = models.CharField(max_length = 30, blank = True, null = True)
    #链接地址
    address = models.CharField(max_length = 50)
    #链接名称
    link_name = models.CharField(max_length = 30)

    def __unicode__(self):
        """
        
        Arguments:
        - `self`:
        """
        return self.link_name
    

#Blog
class Blog(models.Model):
    #类别作为外键
    category = models.ForeignKey(Category)
    #标题
    title = models.CharField(max_length = 50)
    #发布人姓名
    name = models.CharField(max_length = 20, blank = True)
    #发布日期
    pub_date = models.DateTimeField('Date Published')
    #Blog内容
    content = models.TextField()




    def was_published_today(self):
        """
        
        Arguments:
        - `self`:
        """
        return self.pub_date.date() == datetime.date.today()
    was_published_today.short_description = 'Published today?'
    

    def __unicode__(self):
        """
        
        Arguments:
        - `self`:
        """
        return self.title

#     class Admin:
#         js = ['/site_media/AddRichTextEditing.js']
    

#评论
class Comment(models.Model):
    #以Blog为外键
    blog = models.ForeignKey(Blog)
    #姓名
    name = models.CharField(max_length = 50)
    #邮箱
    email = models.EmailField()
    #网址
    webAddress = models.CharField(max_length = 50)
    #评论
    comment = models.TextField()
    #发布日期
    pub_date = models.DateTimeField('Date Published')

    def __unicode__(self):
        """
        
        Arguments:
        - `self`:
        """
        return self.name

#留言
class Message(models.Model):
    #姓名
    name = models.CharField(max_length = 30)
    #邮箱
    email = models.EmailField(null = True)
    #网址
    webAddress = models.CharField(max_length = 50, null = True)
    #留言
    message = models.TextField()
    #发布日期
    pub_date = models.DateTimeField('Date Published')

    def __unicode__(self):
        """
        
        Arguments:
        - `self`:
        """
        return self.name

#Blog图片
class BlogImage(models.Model):
    """For example, say your MEDIA_ROOT is set to '/home/media',
    and upload_to is set to 'photos/%Y/%m/%d'. The '%Y/%m/%d' part
    of upload_to is strftime formatting; '%Y' is the four-digit year,
    '%m' is the two-digit month and '%d' is the two-digit day.
    If you upload a file on Jan. 15, 2007, it will be saved in
    the directory /home/media/photos/2007/01/15.
    """
    blog = models.ForeignKey(Blog)
    name = models.CharField(max_length = 50, null = True)
    image = models.ImageField(upload_to = 'pictures/%Y/%m/%d')
    detail = models.TextField(help_text = 'Detail', blank=True, null=True)


    def __unicode__(self):
        """
        
        Arguments:
        - `self`:
        """
        return self.name

    class Admin:
        js = ['site_media/AddRichTextEditing.js']
    

#名人名言
class Maxim(models.Model):
    #作者
    author = models.CharField(max_length = 30, null = True, blank = True)
    #名言
    words = models.CharField(max_length = 200)

    def __unicode__(self):
        """
        
        Arguments:
        - `self`:
        """
        return self.author
    

from django.contrib import admin

class BlogImageInline(admin.StackedInline):
    model = BlogImage
    extra = 3

class BlogAdmin(admin.ModelAdmin):
    list_display = ('title', 'name', 'category', 'pub_date')
    list_filter = ['pub_date']
    search_fields = ['title']
    date_hierarchy = 'pub_date'

    fieldsets = [
        ('标题', {'fields': ['title']}),
        ('类别', {'fields': ['category']}),
        ('作者姓名', {'fields': ['name']}),
        ('发布日期', {'fields': ['pub_date']}),
        ('发布内容', {'fields': ['content']}),
        ]
    inlines = [BlogImageInline]


    
class MessageAdmin(admin.ModelAdmin):
    list_display = ('name', 'email', 'webAddress', 'message', 'pub_date')
    list_filter = ['pub_date']
    search_fields = ['name']
    date_hierarchy = 'pub_date'

class CommentAdmin(admin.ModelAdmin):
    list_display = ('name', 'blog', 'email', 'webAddress', 'comment', 'pub_date')
    list_filter = ['pub_date']
    search_fields = ['name']
    date_hierarchy = 'pub_date'

class LinkAdmin(admin.ModelAdmin):
    list_display = ('address', 'user', 'link_name')

class MaximAdmin(admin.ModelAdmin):
    list_display = ('author', 'words')
    
admin.site.register(Category)
admin.site.register(Message, MessageAdmin)
admin.site.register(Comment, CommentAdmin)
admin.site.register(Blog, BlogAdmin)
admin.site.register(BlogImage)
admin.site.register(Link, LinkAdmin)
admin.site.register(Maxim, MaximAdmin)
